<?php
namespace api\modules\v1\controllers;

use Yii;
use api\common\controllers\AController;
use common\records\Record;
use common\records\Share;
use common\records\ToyMachine;
use common\records\UserAchievement;
use common\records\Achievement;
use yii\data\ActiveDataProvider;

class ShareController extends AController
{
    public $modelClass = 'common\records\Share';
    /**
     * @api {get} /v1/shares 分享
     * @apiVersion 1.0.0
     * @apiName index
     * @apiGroup shares
     *
     * @apiParam {String} uid 用户id
     *
     */
    public function actionIndex()
    {
        if(!array_key_exists("uid",$_GET) || !array_key_exists("share_id",$_GET))
        {
            $result = array();
            $detail = array();

            $detail['code'] = 400;
            $detail['reason'] = "fields missing";
            $result['result'] = $detail;
            return $result;
        }
        $uid = $_GET['uid'];
        $share_id = $_GET['share_id'];
        $share = Share::find()->where([ 'id'=>$share_id, 'uid'=>$uid])->one();


        
    }

    /*
     @param uid      用户ID
     @param photo    用户头像
     @param nick     用户昵称
     @param toy_name  娃娃名字
     @param toy_photo 娃娃图片
     @param achievement_id   成就id
     */
    public function actionCreate()
    {
        $share = new $this->modelClass(); 
        if(!array_key_exists("uid",$_POST) || !array_key_exists("photo",$_POST)|| !array_key_exists("nick",$_POST) || !array_key_exists("toy_name",$_POST) || !array_key_exists("toy_photo",$_POST) )
        {
            $result = array();
            $detail = array();

            $detail['code'] = 400;
            $detail['reason'] = "字段缺失";
            $result['result'] = $detail;
            return $result;

        }
        $uid = trim($_POST['uid']);
        $photo = trim($_POST['photo']);
        $nick = trim($_POST['nick']);
        $toy_name = trim($_POST['toy_name']);
        $toy_photo = trim($_POST['toy_photo']);
        $achievement_id = 0;
        if(array_key_exists("achievement_id",$_POST))
        {
            $achievement_id = intval(trim($_POST['achievement_id']));
        }

        $share_id = uniqid();
        $share->id = $share_id;
        $share->uid = $uid;
        $share->photo = $photo;
        $share->nick = $nick;
        $share->toy_name= $toy_name;
        $share->toy_photo= $toy_photo;
        $share->achievement_id= $achievement_id;
        $share->share_time = date('Y-m-d H:i:s',time()); 

        $share->save();

        $result = array();
        $detail = array();

        $detail['code'] = 200;
        $detail['reason'] = "创建分享成功";
        $detail['share_id'] = $share_id;
        $result['result'] = $detail;
        
        return $result;
    }
}
